<?php

class TgunController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;


	public function init() {

		$this->commonAction();	

	} 
	//查询机构考勤卡情况列表	
	 public function indexAction() {
		try { 
			 
			 $where='where 1';	
		     $groupid = ( int ) $this->_request->getParam ( 'groups' );
		     $school_id = $this->_request->getParam ( 'school' ); 		
		     $this->view->groups=$groupid; 
		     $this->view->school_id=$school_id;  

			 if($school_id){ 
				 $schoolarray=explode('_',$school_id);  
				 $schoolid=$schoolarray['1'];
			 }
			   
			 if($groupid){
				 $where .=' and  groupid="'.$groupid.'" ';
				 $where_group =' and  groupid="'.$groupid.'" ';
				 $this->view->groupid= $groupid; 
			 }
				 
			 if($schoolid){
				 $where .=' and  schoolid="'.$schoolid.'" '; 
				 $where_school=' and  schoolid="'.$schoolid.'" '; 
				 $this->view->schoolid= $schoolid; 
			 } 
			 
			 $sql='SELECT groupid,groupname from groups where 1'.$where_group;
			 $groupname = $this->_dbAdapter->fetchPairs ($sql);  
			 $this->view->groupname= $groupname; 
			 
			 $sql='SELECT schoolid,schoolname from school where 1'.$where_school;
			 $schoolname = $this->_dbAdapter->fetchPairs ($sql);  
			 $this->view->schoolname= $schoolname; 
			 //print_r($groupname);print_r($schoolname);
			$count = 10;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			$this->view->pageNum= $curpage;			
    		$offset = ($curpage - 1) * $count; 

			 $sql="SELECT SQL_CALC_FOUND_ROWS groupid,schoolid,COUNT(*) as num,count(case when isdelete=0 then 'used' end) used,count(case when isdelete=1 then 'used' end) del from temperaturegun ".$where." GROUP BY schoolid limit $offset,$count";
			 $result = $this->_dbAdapter->fetchAssoc ($sql);  
			 $this->view->result= $result;
			 //print_r($result);print_r($outdatetime);exit;
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    			$page->setvar ( array ('groups' => $groupid,'school'=>$schoolid) );
				$this->view->page= $page->getNumPage ();
				
		} catch ( Exception $e ) {
			 $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	 public function detailedAction() {
		try {  	
		     $where='where 1';	
		     $groupid = ( int ) $this->_request->getParam ( 'groups' );
		     $school_id = $this->_request->getParam ( 'school' ); 		
		     $schoolid = $this->_request->getParam ( 'schoolid' ); 		
		     $this->view->groups=$groupid; 
		     $this->view->school_id=$school_id;  
		     $this->view->schoolid=$schoolid;  

			 if($school_id){ 
				 $schoolarray=explode('_',$school_id);  
				 $schoolid=$schoolarray['1'];
			 }
			   
			 if($groupid){
				 $where .=' and  groupid="'.$groupid.'" ';
				 $where_group =' and  groupid="'.$groupid.'" ';
				 $this->view->groupid= $groupid; 
			 }
				 
			 if($schoolid){
				 $where .=' and  schoolid="'.$schoolid.'" '; 
				 $where_school=' and  schoolid="'.$schoolid.'" '; 
				 $this->view->schoolid= $schoolid; 
			 } 
			 
			 $sql='SELECT groupid,groupname from groups where 1'.$where_group;
			 $groupname = $this->_dbAdapter->fetchPairs ($sql);  
			 $this->view->groupname= $groupname; 
			 
			 $sql='SELECT schoolid,schoolname from school where 1'.$where_school;
			 $schoolname = $this->_dbAdapter->fetchPairs ($sql);  
			 $this->view->schoolname= $schoolname; 
			 //print_r($groupname);print_r($schoolname);
			$count = 10;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			$this->view->pageNum= $curpage;			
    		$offset = ($curpage - 1) * $count; 

			 $sql="SELECT SQL_CALC_FOUND_ROWS id,tid,groupid,schoolid,isdelete,from_unixtime(addtime,'%Y-%m-%d') as addtime from temperaturegun ".$where." limit $offset,$count";
			 $result = $this->_dbAdapter->fetchAll ($sql);  
			 $this->view->result= $result;
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    			$page->setvar ( array ('groups' => $groupid,'school'=>$schoolid) );
				$this->view->page= $page->getNumPage ();
		} catch ( Exception $e ) {
			 $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	

   public function groupsAction() {
		try { 
		 $p =$this->_request->getPost ();
         $sql="select * from  groups ";
	     $result = $this->_dbAdapter->fetchAll ($sql);
		 
		 $str.='<option value="0" >==请选择机构==</option>';
		 for($i = 0; $i<count($result); $i++)
		{
			$str .= "<option";
			if($p['jigouid']==$result[$i]['groupid']){
				$str .=" selected=selected ";
			}
	      $str .= " value='".$result[$i]['groupid']."'>".$result[$i]['groupname']."</option>";
		}
		  
	    echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//获取所有的校园列表
	  public function schoollistAction() {
		try { 
           if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		  $p =$this->_request->getPost (); 
		   
		  $array=explode('_',$p['sid']);
		  
		  $sql="SELECT * FROM `groups` where groupid='".$p['id']."' ";
		  $result = $this->_dbAdapter->fetchRow ($sql);
		    $sql="select * from  school where groupid='".$p['id']."' ";   
			$schoollist=$this->_dbAdapter->fetchAll ($sql);
	                 
            $str.='<option value="0" >==请选择学校==</option>';
			// echo $p['id'];   exit;
		 for($i = 0; $i<count($schoollist); $i++)
		{
		  $str .= "<option ";
		  
		  if ($array['1']==$schoollist[$i]['schoolid']){
		      $str .=" selected=selected";
		  }
		   $str .= " value='".$p['id'].'_'.$schoollist[$i]['schoolid']."'>";
		   $str .= $schoollist[$i]['schoolname']."</option>";
		   
		}
		   
	      echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}}

